# -*- coding: utf-8 -*-
#
# Copyright (c) 2025 Huawei Technologies Co., Ltd.
# openUBMC is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#         http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.

"""
现代化扩展机制

提供简洁、类型安全的扩展接口，支持多种扩展方式：
1. 简单回调函数
2. 自定义 Emitter 类
3. 装饰器语法
"""

from typing import Protocol, Union, Callable
from ...ir import IRNode
from .emitter import BaseEmitter, EmitResult
from .context import CodeGenContext


class CodeGenerator(Protocol):
    """代码生成器协议"""

    def generate_expression(
        self, node: IRNode, context: CodeGenContext, parent_precedence: int = 0
    ) -> str:
        """生成表达式代码"""
        ...

    def generate_statement(self, node: IRNode, context: CodeGenContext) -> str:
        """生成语句代码"""
        ...


# 简单回调签名
SimpleCallback = Callable[
    [IRNode, CodeGenerator, CodeGenContext], Union[str, EmitResult]
]

# 扩展回调类型
EmitterCallback = Union[SimpleCallback, BaseEmitter]
